const dataKit = require( "./dataKit")
const dateKit = require( "./dateKit");
const util = require( "./util");


/**
 * 注意：使用await的方法必须加上async，不然会报错！！
 */
module.exports = {

  /**
    * 获取商户唯一的商户AccessToken
    */
   async getDefaultMerchantAccessToken(context){
      //过滤条件
      const filter = {};
      let merchant = await dataKit.findOne(context,"merchant",filter);
      return merchant.accessToken;
   },


  /**
     * 判断用户是否是会员
     * @param {*} context 上下文
     * @param {*} mix_nick 用户 mix_nick
     */
    async isTaobaoMember(context,mix_nick,openId) {
        const accessToken = await this.getDefaultMerchantAccessToken(context);
        const identity = await context.cloud.topApi.invoke({
            api:'taobao.crm.member.identity.get',
            autoSession: false,
            data:{
                session: accessToken,
                mix_nick: mix_nick
            }
        });
        if (identity.result.member_info) {// 是会员
            const userData = await dataKit.findOne(context,"user_data",{openId:openId,type:7});
            if(!userData){
              await this.buriedPoint(context,openId,{userNick:context.userNick,type:7,remark:"入会",data:''});
            }
            return true;
        }else {
            return false;
        }
    },


    /**
    * 根据openId获取会员信息
    * @param {*} openId 
    */
   async getUserInfoByOpenId(context,openId){
      if(!openId){
         console.info("getUserInfoByOpenId --> openId是必须的");
         return {code: 500, msg: "getUserInfoByOpenId --> openId是必须的"};
      }
      //过滤条件
      const filter = {openId: openId};
      let userInfo =await dataKit.findOne(context,"user_info",filter);
      return userInfo;
   },

   /**
    * 修改眼型
    * @param {*} context 
    * @param {*} openId 
    * @param {*} eyesType 
    */
   async editAReyes(context,openId,eyesType){
     if(!openId){
         console.info("editAReyes --> openId是必须的");
         return {code: 500, msg: "editAReyes --> openId是必须的"};
     }
     let result = await dataKit.update(context, "user_info", {openId:openId},{isEyes:true,eyesType:eyesType});
     return {code:200,msg:"操作成功",data:result};
   },

   /**
     * 埋点
     * @param {埋点} context 
     */
    async buriedPoint(context,openId,userData){
      var buriedPoint = {
                openId:openId,
                userNick:userData.userNick,
                type:userData.type,
                remark:userData.remark,
                data:userData.data,
                createTime:dateKit.getCurrentDateTime()
              };
      let result = await dataKit.insertOne(context,"user_data",buriedPoint);
      return {code:200,msg:"埋点成功",data:result}
    },

    /**
     * 商品加购
     * @param {*} context 
     * @param {*} openId 
     * @param {*} userNick 
     * @param {*} productId 
     */
    async goodsPurchased(context,openId,userNick,productId){
      var record = await dataKit.findOne(context,"task_record",{openId:openId,productId:productId,type:'browseProduct'});
      if(record != null){
        return {code:500,msg:'宝贝已加购'}
      }
      var taskRecord = {
        openId:openId,//用户唯一标识
        userNick:userNick,//用户淘宝昵称
        type:'browseProduct',	//任务类型：
        productId:productId,//商品id（浏览商品的时候用到，其他情况为空）
        remark:'加购宝贝',	//备注
        participantOpenId:'',	//参与者openId
        participantUserNick:'',	//参与者昵称
        participantHeadIcon:'',	//参与者头像
        createTime:dateKit.getCurrentDateTime(),	//保存时间
      }
      await dataKit.insertOne(context,'task_record',taskRecord);
      await this.buriedPoint(context,openId,{userNick:userNick,type:8,remark:"商品加购",data:productId});
      return {code:200,msg:"加购宝贝成功"};
    }


}